# -*- coding: utf-8 -*-

from app.model.common.member import Member as MemberModel
from app import db
from app.inc.functions import rt, md5


class Member:
    @staticmethod
    def change_pwd(member_id=0, old_pwd='', new_pwd=''):
        """ 更改密码 """

        member_id = int(member_id)
        if member_id < 1:
            return rt(1, '参数错误')

        old_pwd = old_pwd.strip()
        if old_pwd == '':
            return rt(1, '缺少旧密码')

        new_pwd = new_pwd.strip()
        if new_pwd == '':
            return rt(1, '缺少新密码')

        mm = MemberModel.query.filter_by(member_id=member_id).first()

        # 密码是否匹配
        if md5(old_pwd) == mm.pwd:
            mm.pwd = md5(new_pwd)
            db.session.add(mm)
            db.session.commit()
            return rt(0, 'ok')
        else:
            return rt(1, '原密码错误')
